package streamforce.workload;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import streamforce.Stat;
import streamforce.crypto.event.StockEvent;
import streamforce.crypto.owner.EPOwnerClient;

public class SingleFileEncryptorAgg {
	public static void main(String[] args) throws IOException{
		int streamId = new Integer(args[0]).intValue();
		int index = new Integer(args[1]).intValue() - 1; 		
		Properties prop = new Properties();
		int aggType = 0; 
		int userMaxId=0; 
		try {
			prop.load(new FileInputStream("attributes.properties"));
			userMaxId = new Integer(prop.getProperty("maxUserId")).intValue();
			aggType = new Integer(prop.getProperty("aggregateType")).intValue();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}				    	
		
		EPOwnerClient owner = new EPOwnerClient(streamId); 
		
		if (args.length <= 1 || !args[1].equals("-ne")) {

			switch (aggType) {
			case -1:
				//EPOwnerClient.initSimpleClient(owner, streamId, 0);
				break;			
			case 1:
				//EPOwnerClient.initAggregateClient_1(owner, streamId, 0, 4);				
				break;
			case 2:
				//EPOwnerClient.initAggregateClient_2(owner, streamId, 0, 4);
				break;
			case 3:
				//EPOwnerClient.initAggregateClient_3(owner, streamId, 0, 4, 100000);
				break;
			}			
			
			int ts = 0; 
			ObjectOutputStream oos;// = new ObjectOutputStream(new FileOutputStream("enc_file_"+streamId));
			ArrayList<StockEvent> ses = new ArrayList<StockEvent>();
			int hour = 0; 
			Random rand = new Random(streamId); 
			int price, volume; 
			do{				
				price = rand.nextInt(20);
				volume = rand.nextInt(100);
				if (ts >= (index * 400) && (ts < (index + 1) * 400)) {
					//System.out.format("(%d,%d,%d,%d,%d)\n",ts,hour%24,streamId,price,volume); 					
					StockEvent sd = owner.encryptData(ts, hour % 24, streamId,
							price, volume);
					ts++;
					if (ts % 60 == 0)
						hour++;
					
					ses.add(sd);
					if (ts % 400 == 0) {
						if (aggType==2)
						oos = new ObjectOutputStream(new FileOutputStream(
								"/DATA/NewEncFiles/enc_file_" + streamId + "_"
										+ (ts / 400)));
						else if (aggType==1)
							oos = new ObjectOutputStream(new FileOutputStream(
									"/DATA/NewEncFiles/enc_file_agg1_" + streamId + "_"
											+ (ts / 400)));
						else
							oos = new ObjectOutputStream(new FileOutputStream(
									"/DATA/NewEncFiles/enc_file_agg3_" + streamId + "_"
											+ (ts / 400)));
						oos.writeObject(ses);
						oos.close();
						ses = new ArrayList<StockEvent>();
					}					
				}
				else{
					ts++;
					if (ts % 60 == 0)
						hour++;
				}
				//serialize to file
				//oos.writeObject(sd); 
			}while (ts<10000);
			//oos.writeObject(ses);
			//oos.close(); 
			EPOwnerClient.initSimpleClient(owner, streamId, 0);
		}
	}
}
